package com.swotplus.admin.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.framework.web.BaseController;
import com.swotplus.common.annotation.Log;
import com.swotplus.common.constant.UserConstants;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.common.enums.BusinessType;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.framework.shiro.ShiroUtils;
import com.swotplus.system.sys.entity.SysPost;
import com.swotplus.system.sys.service.SysPostService;
import com.swotplus.system.sys.vo.SysPostVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位信息操作处理
 * 
 * @author hugang
 */
@Controller
@RequestMapping("/system/post")
public class SysPostController extends BaseController
{
    private String prefix = "system/post";

    @Autowired
    private SysPostService sysPostService;

    /**
     * 跳转前端用户页面
     * @return
     */
    @RequiresPermissions("system:post:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/post";
    }

    @RequiresPermissions("system:post:list")
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult list(SysPostVo vo)
    {
        try
        {
            Page<SysPost> page=new Page<>(vo.getPage(),vo.getLimit());
            QueryWrapper<SysPost> sysPostQueryWrapper=new QueryWrapper<>();
            if(StringUtils.isNotBlank(vo.getPostName())){
                sysPostQueryWrapper.eq("post_name",vo.getPostName());
            }
            if(StringUtils.isNotBlank(vo.getPostCode())){
                sysPostQueryWrapper.eq("post_code",vo.getPostCode());
            }
            Page<SysPost> pageList= sysPostService.page(page,sysPostQueryWrapper);
            return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
        } catch (Exception e)
        {
            return AjaxResult.warn(e.getMessage());
        }

    }


    @RequiresPermissions("system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            List<String> ListStr = Arrays.asList(ids.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
            return toAjax(sysPostService.removeByIds(ListStr));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysPost post)
    {
        QueryWrapper<SysPost> queryNameWrapper=new QueryWrapper<>();
        queryNameWrapper.eq("post_name",post.getPostName());

        QueryWrapper<SysPost> queryCodeWrapper=new QueryWrapper<>();
        queryCodeWrapper.eq("post_code",post.getPostCode());
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(sysPostService.count(queryNameWrapper)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(sysPostService.count(queryCodeWrapper)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysPostService.save(post));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") String postId, ModelMap mmap)
    {
        mmap.put("post", sysPostService.getById(postId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysPost post)
    {
        QueryWrapper<SysPost> queryNameWrapper=new QueryWrapper<>();
        queryNameWrapper.eq("post_name",post.getPostName());

        QueryWrapper<SysPost> queryCodeWrapper=new QueryWrapper<>();
        queryCodeWrapper.eq("post_code",post.getPostCode());
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(sysPostService.count(queryNameWrapper)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(sysPostService.count(queryCodeWrapper)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysPostService.updateById(post));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(SysPost post)
    {
        QueryWrapper<SysPost> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("post_name",post.getPostName()).eq("post_code",post.getPostCode());
        if(sysPostService.count(queryWrapper)>0){
          return   UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(SysPost post)
    {
        QueryWrapper<SysPost> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("post_code",post.getPostCode());
        if(sysPostService.count(queryWrapper)>0){
            return   UserConstants.POST_CODE_NOT_UNIQUE;
        }
        return UserConstants.POST_CODE_UNIQUE;

    }
}
